//
//  LoginViewController.m
//  SkinerZTZ
//
//  Created by cts on 2017/12/7.
//  Copyright © 2017年 hhjiang. All rights reserved.
//

#import "LoginViewController.h"
#import "Header.h"
#import "TLNetworking.h"

#import "AppDelegate.h"



@interface LoginViewController ()

@property(nonatomic,strong)UITextField *nameT;
@property(nonatomic,strong)UITextField *passwordT;

@end

@implementation LoginViewController
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"登录";
    self.view.backgroundColor = [UIColor whiteColor];
    [self createUI];
    
    
}

-(void)createUI{
    
   
    UIImageView *logo = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, kScreenWidth, kScreenWidth/2+20)];
    logo.image = [UIImage imageNamed:@"我的积分-背景"];
  //  logo.backgroundColor = [UIColor blueColor];
   
    [self.view addSubview:logo];
    UILabel *titleL = [[UILabel alloc] initWithFrame:CGRectMake(0, kScreenWidth/4-25-+10, kScreenWidth, 50)];
    titleL.text = @"天玛仕";
    titleL.textColor = [UIColor whiteColor];
    titleL.textAlignment = NSTextAlignmentCenter;
  //  titleL.font = kFONT_HEITI_TC(80);
    titleL.font = [UIFont boldSystemFontOfSize:30];
    [logo addSubview:titleL];
    UIImageView *phoneImage = [[UIImageView alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(logo.frame)+45, 30, 30)];
    phoneImage.image = [UIImage imageNamed:@"手机号"];
    [self.view addSubview:phoneImage];
    _nameT = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(phoneImage.frame)+5, CGRectGetMaxY(logo.frame)+45, kScreenWidth -60, 30)];
    _nameT.font = kFONT_HEITI_TC(15);
    _nameT.placeholder = @"请输入账号";
    //_nameT.keyboardType = UIKeyboardTypeNumberPad;
    _nameT.textColor = [UIColor blackColor];
   // [_nameT setValue:color(197, 207, 220) forKeyPath:@"_placeholderLabel.textColor"];
    [self.view addSubview:_nameT];
    UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(phoneImage.frame)+5, CGRectGetMaxY(_nameT.frame)+3, kScreenWidth -CGRectGetMaxX(phoneImage.frame)-10, 0.6)];
    line.backgroundColor = color(240, 240, 240);
    [self.view addSubview:line];
    
    UIImageView *mimaImage = [[UIImageView alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(line.frame)+35, 30, 30)];
    mimaImage.image = [UIImage imageNamed:@"密码"];
    [self.view addSubview:mimaImage];
    
    _passwordT = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(phoneImage.frame)+5, CGRectGetMaxY(line.frame)+35, kScreenWidth -60, 30)];
    _passwordT.font = kFONT_HEITI_TC(15);
    _passwordT.placeholder = @"请输入密码";
    _passwordT.textColor = [UIColor blackColor];
   // [_passwordT setValue:color(197, 207, 220) forKeyPath:@"_placeholderLabel.textColor"];
    _passwordT.secureTextEntry = YES;
    [self.view addSubview:_passwordT];
    UILabel *line1 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(phoneImage.frame)+5, CGRectGetMaxY(_passwordT.frame)+3, kScreenWidth -CGRectGetMaxX(phoneImage.frame)-10, 0.6)];
    line1.backgroundColor = color(240, 240, 240);
    [self.view addSubview:line1];
    
    //登录
    UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    loginBtn.frame = CGRectMake(30, CGRectGetMaxY(line1.frame)+40, kScreenWidth - 60, 44);
    loginBtn.backgroundColor = KCommonColor;
    loginBtn.layer.cornerRadius = 22;
    loginBtn.clipsToBounds = YES;
    [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    loginBtn.titleLabel.font = kFONT_HEITI_TC(16);
    [loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [loginBtn addTarget:self action:@selector(loginClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginBtn];
    
    //zhuce
//    UIButton *regsitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    regsitBtn.frame = CGRectMake(kScreenWidth - 85, CGRectGetMaxY(loginBtn.frame)+15, 60, 26);
//    [regsitBtn setTitle:@"注册" forState:UIControlStateNormal];
//    [regsitBtn setTitleColor:color(51, 51, 51) forState:UIControlStateNormal];
//    regsitBtn.titleLabel.font = kFONT_HEITI_TC(15);
//    [regsitBtn addTarget:self action:@selector(regsitClick) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:regsitBtn];
    //忘记密码
//    UIButton *forgetBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    forgetBtn.frame = CGRectMake(25, CGRectGetMaxY(loginBtn.frame)+15, 100, 26);
//    [forgetBtn setTitle:@"忘记密码?" forState:UIControlStateNormal];
//    forgetBtn.titleLabel.font = kFONT_HEITI_TC(15);
//    [forgetBtn setTitleColor:color(51, 51, 51) forState:UIControlStateNormal];
//    [forgetBtn addTarget:self action:@selector(forgetClick) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:forgetBtn];
    
//    UILabel *line2 = [[UILabel alloc] initWithFrame:CGRectMake(50, CGRectGetMaxY(forgetBtn.frame)+26+24, kScreenWidth -100, 0.6)];
//    line2.backgroundColor = color(240, 240, 240);
//    [self.view addSubview:line2];
//    UILabel *loginL = [[UILabel alloc] initWithFrame:CGRectMake((kScreenWidth -130)/2, CGRectGetMaxY(forgetBtn.frame)+13.3+24, 130, 26)];
//    loginL.textColor = [UIColor lightGrayColor ];
//    loginL.textAlignment = NSTextAlignmentCenter;
//    loginL.backgroundColor = [UIColor whiteColor];
//    loginL.text = @"其他登录方式";
//    loginL.font = kFONT_HEITI_TC(13);
//    [self.view addSubview:loginL];
//    CGFloat kspace = (kScreenWidth - 50 -80)/2;
    //qq
//    UIButton *qqBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    qqBtn.frame = CGRectMake(kspace, CGRectGetMaxY(loginL.frame)+20, 40, 40);
//    [qqBtn setImage:[UIImage imageNamed:@"微信"] forState:UIControlStateNormal];
//    [qqBtn addTarget:self action:@selector(qqBtnClick) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:qqBtn];
//    //wechat
//    UIButton *wechatBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    wechatBtn.frame = CGRectMake(kspace + 90, CGRectGetMaxY(loginL.frame)+20, 40, 40);
//    [wechatBtn setImage:[UIImage imageNamed:@"QQ"] forState:UIControlStateNormal];
//    [wechatBtn addTarget:self action:@selector(wechatBtnClick) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:wechatBtn];
}
#pragma mark - Click
//注册
-(void)regsitClick{
//    ResigeViewController *ge = [[ResigeViewController alloc] init];
//    [self.navigationController pushViewController:ge animated:YES];
}
//忘记密码
-(void)forgetClick{
//    ForgetPWController *vc = [[ForgetPWController alloc] init];
//    [self.navigationController pushViewController:vc animated:YES];
}
//登录
-(void)loginClick{
  
    //3.判断密码是否为空
    if (self.nameT.text.length==0) {
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.label.text = @"账号不能为空";
        [hud hideAnimated:YES afterDelay:0.8f];
        return;
    }
    //4.判断密码是否为空
    if (self.passwordT.text.length==0) {
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.label.text = @"密码不能为空";
        [hud hideAnimated:YES afterDelay:0.8f];
        return;
    }
    //验证手机号码是否合法
//    NSString *phoneReg =@"^1(3[0-9]|5[0-35-9]|7[6-8]|8[0-9])\\d{8}$";
//    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phoneReg];
//    BOOL isPhoneValid = [regextestmobile evaluateWithObject:self.nameT.text];
//    if (!isPhoneValid) {
//
//        return;
//    }
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    TLNetworking  *TLNet = [TLNetworking sharedInstance];
    NSLog(@"Klogin = %@",Klogin);
    NSDictionary *dict = @{@"code":self.nameT.text,@"password":self.passwordT.text};

    [TLNet GET:Klogin parameters:dict Withsuccess:^(id responseObject) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];

        NSLog(@"responseObject = %@",responseObject);
        if ([responseObject[@"status"] isEqualToString:@"200"]) {
           // NSDictionary *dict = responseObject[@"data"];
     
            [UnityTool setStringValueForConfigurationKey:@"sid" WithValue:responseObject[@"sid"]];
           
            AppDelegate *app = (AppDelegate*)[UIApplication sharedApplication].delegate;
            [app setTabarViewControlle];
           
        }else{
            MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            hud.mode = MBProgressHUDModeText;
            hud.label.text = responseObject[@"error"];
            [hud hideAnimated:YES afterDelay:0.8f];
        }

    } WithFailure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
       NSLog(@"error = %@",error);


    }];
    
}
//qq
-(void)qqBtnClick{
    
    
}
//微信
-(void)wechatBtnClick{

    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}



@end
